using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;



namespace Microsoft.NETMF.BikeComputer.Application
{
    public class RideView : View
    {

        public RideView(RideDataModel model, ComputerController controller)
            : base(model)
        {

            _model = model;
            _controller = controller;

            if (_model.Units.Current().Label == Resources.GetString(Resources.StringResources.MetricUnits))
                _metric = true;
            else
                _metric = false;

            StackPanel ridePanel = new StackPanel(Orientation.Vertical);
            ridePanel.HorizontalAlignment = HorizontalAlignment.Center;
            ridePanel.VerticalAlignment = VerticalAlignment.Center;

            ////////////////////////////////////////////////////////////////////////////////////////
            //  Top Area
            //

            StackPanel  _topPanel = new StackPanel(Orientation.Horizontal);
            _topPanel.HorizontalAlignment = HorizontalAlignment.Center;
            _topPanel.VerticalAlignment = VerticalAlignment.Center;

            Text _date = new Text(Resources.GetFont(Resources.FontResources.miramob), DateTime.Today.Month.ToString() + "/" + DateTime.Today.Day.ToString() + "/" + DateTime.Today.Year.ToString());
            _date.SetMargin(0, 0,40, 0);

            Text _time = new Text(Resources.GetFont(Resources.FontResources.miramob), DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString());
            _time.SetMargin(0, 0, 40, 0);

            _temperature = new Text(Resources.GetFont(Resources.FontResources.miramob),  "n/a C");
            _topPanel.Children.Add(_date);
            _topPanel.Children.Add(_time);
            _topPanel.Children.Add(_temperature);



            ////////////////////////////////////////////////////////////////////////////////////////
            //  Middle Area
            //
            StackPanel _midPanel = new StackPanel(Orientation.Horizontal);
   
            StackPanel _leftMidPanel = new StackPanel(Orientation.Vertical);
            _leftMidPanel.VerticalAlignment = VerticalAlignment.Top;
            _leftMidPanel.SetMargin(0,0,0,50);


            _elapsedTime = new Text(Resources.GetFont(Resources.FontResources.nina48), "n/a");
            _elapsedTime.HorizontalAlignment = HorizontalAlignment.Center;
            Text _etLabel = new Text(Resources.GetFont(Resources.FontResources.miramob), Resources.GetString(Resources.StringResources.etLabel));
            _etLabel.HorizontalAlignment = HorizontalAlignment.Center;

            _leftMidPanel.Children.Add(_elapsedTime);
            _leftMidPanel.Children.Add(_etLabel);

            StackPanel _midMidPanel = new StackPanel(Orientation.Vertical);
            _midMidPanel.SetMargin(30, 50, 30, 0);

            _speed = new Text(Resources.GetFont(Resources.FontResources.nina48), "n/a");
            _speed.VerticalAlignment = VerticalAlignment.Bottom;
            if (_metric)
                _speedLabel = new Text(Resources.GetFont(Resources.FontResources.miramob), Resources.GetString(Resources.StringResources.kphLabel));
            else
                _speedLabel = new Text(Resources.GetFont(Resources.FontResources.miramob), Resources.GetString(Resources.StringResources.mphLabel));
            _speedLabel.VerticalAlignment = VerticalAlignment.Bottom;
            _speedLabel.HorizontalAlignment = HorizontalAlignment.Center;

            _midMidPanel.Children.Add(_speed);
            _midMidPanel.Children.Add(_speedLabel);

            StackPanel _rightMidPanel = new StackPanel(Orientation.Vertical);
            _rightMidPanel.SetMargin(20, 0, 0, 50);

            _cadence = new Text(Resources.GetFont(Resources.FontResources.nina48), "n/a");
            _cadence.HorizontalAlignment = HorizontalAlignment.Center;

            Text _rpmLabel = new Text(Resources.GetFont(Resources.FontResources.miramob), Resources.GetString(Resources.StringResources.rpmLabel));
            _rpmLabel.HorizontalAlignment = HorizontalAlignment.Center;

            _rightMidPanel.Children.Add(_cadence);
            _rightMidPanel.Children.Add(_rpmLabel);


            _midPanel.Children.Add(_leftMidPanel);
            _midPanel.Children.Add(_midMidPanel);
            _midPanel.Children.Add(_rightMidPanel);


            ////////////////////////////////////////////////////////////////////////////////////////
            //  Bottom Area
            //
            StackPanel _bottomPanel = new StackPanel(Orientation.Horizontal);

            StackPanel _leftBottomPanel = new StackPanel(Orientation.Vertical);
            _leftBottomPanel.SetMargin(20, 0, 50, 0);

            _incline = new Text(Resources.GetFont(Resources.FontResources.nina48), "n/a");
            _incline.HorizontalAlignment = HorizontalAlignment.Center;

            Text _inclineLabel = new Text(Resources.GetFont(Resources.FontResources.miramob), Resources.GetString(Resources.StringResources.inclineLabel));
            _inclineLabel.HorizontalAlignment = HorizontalAlignment.Center;

            _leftBottomPanel.Children.Add(_incline);
            _leftBottomPanel.Children.Add(_inclineLabel);

            StackPanel _rightBottomPanel = new StackPanel(Orientation.Vertical);
            _rightBottomPanel.SetMargin(120, 0, 0, 0);

            _distance = new Text(Resources.GetFont(Resources.FontResources.nina48), _model.getDistance().ToString());
            _distance.HorizontalAlignment = HorizontalAlignment.Center;
            if (_metric)
                _distanceLabel = new Text(Resources.GetFont(Resources.FontResources.miramob), Resources.GetString(Resources.StringResources.kilometerLabel));
            else
                _distanceLabel = new Text(Resources.GetFont(Resources.FontResources.miramob), Resources.GetString(Resources.StringResources.milesLabel));
            _distanceLabel.HorizontalAlignment = HorizontalAlignment.Center;

            _rightBottomPanel.Children.Add(_distance);
            _rightBottomPanel.Children.Add(_distanceLabel);

            _bottomPanel.Children.Add(_leftBottomPanel);
            _bottomPanel.Children.Add(_rightBottomPanel);

            ///////////////////////////////////////////////////////////////////////////////////////
            //  Paused Ride Panel
            //
            _pausedPanel = new StackPanel(Orientation.Vertical);
            _pausedPanel.Visibility = Visibility.Hidden;

            Text _pausedText = new Text(Resources.GetFont(Resources.FontResources.miramob), Resources.GetString(Resources.StringResources.PausedText));
            _pausedText.HorizontalAlignment = HorizontalAlignment.Center;
            _pausedText.SetMargin(0, 10, 0, 0);

            _pausedPanel.Children.Add(_pausedText);

            ///////////////////////////////////////////////////////////////////////////////////////
            //  panel Setup
            //

            ridePanel.Children.Add(_topPanel);
            ridePanel.Children.Add(_midPanel);
            ridePanel.Children.Add(_bottomPanel);
            ridePanel.Children.Add(_pausedPanel);

            Child = ridePanel;

            etTimer = new DispatcherTimer();
            etTimer.Tick += new EventHandler(etTimerEventHandler);
            etTimer.Interval = new TimeSpan(0, 0, 1);
            etTimer.Start();   // this will work for now but we need to make this start and stop with this view


            ///////////////////////////////////////////////////////////////////////////////////////
            // attach event handler to the controller
            // 

            _model.OnRideDataChanged += new RideDataModel.RideDataChangedEventHandler(OnRideDataChanged);

        }



      
        //--//

        #region gesture handlers

        public void PauseRide()
        {
            _pausedPanel.Visibility = Visibility.Visible;
           this.Invalidate();
        }

        public void ResumeRide()
        {
            _pausedPanel.Visibility = Visibility.Hidden;
           this.Invalidate();
        }


        #endregion

        #region Event Handlers
        /*
        
        private void displayTimerEventHandler(object o, EventArgs e)
        {

            _model.processData();
   
            //_incline.TextContent = _model.Incline.ToString("F1");
            ;            //temporary
            _incline.TextContent = _controller._inclineSensor.GetIncline().ToString("F1");                

            _incline.TextContent = _model.Incline.ToString("F1");
            _temperature.TextContent = _model.Temperature.ToString("F1") + " C";

            // UPDATE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                _speed.TextContent= _model.getSpeed().ToString("F1");
                _cadence.TextContent = _model.getCadence().ToString("F0");
                _distance.TextContent = _model.getDistance().ToString("F1");
                _elapsedTime.TextContent = _controller.getElapsedTime();
        }
         * */


        private void etTimerEventHandler(object o, EventArgs e)
        {
            _elapsedTime.TextContent = _controller.getElapsedTime();
        }
        #endregion


        private void OnRideDataChanged(double incline, double speed, double distance, double cadence, double temperature)
        {
            _incline.Dispatcher.BeginInvoke(new DispatcherOperationCallback(this.UpdateIncline), incline);
            _speed.Dispatcher.BeginInvoke(new DispatcherOperationCallback(this.UpdateSpeed), speed);
            _distance.Dispatcher.BeginInvoke(new DispatcherOperationCallback(this.UpdateDistance), distance);
            _cadence.Dispatcher.BeginInvoke(new DispatcherOperationCallback(this.UpdateCadence), cadence);
            _temperature.Dispatcher.BeginInvoke(new DispatcherOperationCallback(this.UpdateTemperature), temperature);

        }

        private object UpdateIncline(object arg) {
            _incline.TextContent = ((double)arg).ToString("F1"); return null;
        }
        private object UpdateSpeed(object arg)
        {
            _speed.TextContent = ((double)arg).ToString("F1"); return null;
        }
        private object UpdateDistance(object arg)
        {
            _distance.TextContent = ((double)arg).ToString("F1"); return null;
        }
        private object UpdateCadence(object arg)
        {
            _cadence.TextContent = ((double)arg).ToString("F1"); return null;
        }
        private object UpdateTemperature(object arg)
        {
            _temperature.TextContent = ((double)arg).ToString("F1"); return null;
        }

        //--//
        StackPanel _pausedPanel;
        Text _speed;
        Text _cadence;
        Text _distance;
        Text _incline;
        Text _temperature;
        //private DispatcherTimer displayTimer;
        private DispatcherTimer etTimer;
        private RideDataModel _model;
        private ComputerController _controller;
        Text _distanceLabel;
        Text _speedLabel;
        bool _metric;
        Text _elapsedTime;
    }
}

